﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService

Partial Class Control_MSDS_Section06
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Label
        Translate()
        If Not IsPostBack Then
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "duyjs", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "duyjs", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If

            hidMSDSID.Value = IIf(IsNothing(Request.QueryString("Id")), 0, CInt(Request.QueryString("Id")))
            ShowData()
        End If
    End Sub
    Protected Sub Translate()
        lblSection06Header.Text = getTranslate("lblSection06Header", Session("LanguageId"), lblSection06Header.Text)
        lblS611.Text = getTranslate("lblS611", Session("LanguageId"), lblS611.Text)
        lblS621.Text = getTranslate("lblS621", Session("LanguageId"), lblS621.Text)
    End Sub
    Protected Sub ShowData()
        Using data As New VSEntities
            'Tiêu đề doanh nghiệp
            Dim lang = CInt(Session("LanguageId"))
            Dim Id As Integer = hidMSDSID.Value
            Dim p As MSDSSection06 = (From a In data.MSDSSection06 Where a.MSDSId = Id And a.LanguageId = lang).SingleOrDefault
            If Not IsNothing(p) Then
                hidID.Value = p.Section06Id

                'Content
                txtS611.Text = IIf(IsNothing(p.S611) = True, "", p.S611)
                txtS621.Text = IIf(IsNothing(p.S621) = True, "", p.S621)
                txtS631.Text = IIf(IsNothing(p.S631) = True, "", p.S631)

            End If
         
        End Using
    End Sub
 #End Region
#Region "Event for control "
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Using data As New VSEntities
            Try
                Dim p As MSDSSection06
                p = (From q In data.MSDSSection06
                Where q.Section06Id = hidID.Value
                  ).FirstOrDefault()
                Dim _isNew = True
                If Not p Is Nothing Then
                    _isNew = False
                Else
                    p = New MSDSSection06
                End If
                With p
                    .S611 = txtS611.Text.Trim
                    .S621 = txtS621.Text.Trim
                    .S631 = txtS631.Text.Trim
                    .LanguageId = Session("LanguageId")
                    .MSDSId = hidMSDSID.Value
                End With
                If _isNew Then
                    p.Created = Now
                    p.Creator = Session("UserName")
                    data.MSDSSection06.AddObject(p)
                Else
                    p.Modified = Now
                    p.Modifier = Session("UserName")
                End If
                data.SaveChanges()

                Insert_App_Log(If(_isNew, "Insert", "Update") & "  MSDS Section06:" & hidMSDSID.Value & "", Function_Name.MSDSOriginal, If(_isNew, Audit_Type.Create, Audit_Type.Edit), Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                Excute_Javascript("AlertboxRedirect('Lưu dữ liệu thành công.','../../Page/MSDS/Section07.aspx?Id=" & hidMSDSID.Value & "');", Me.Page, True)
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
            Finally
                GC.Collect()
            End Try
        End Using
    End Sub
    Protected Sub btnPrevious_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        Response.Redirect("../../Page/MSDS/Section05.aspx?Id=" & hidMSDSID.Value)
    End Sub
#End Region

    
End Class
